/**
 * 隐患类型 列表页
 *
 * 作者 wanghw
 * 日期 2024-08-29 15:10:21
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardTypeOperation'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardTypeOperation-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'hazardTypeOperationTable',
            url: ctx + 'hazardTypeOperation/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'typeOperationId', title: '主键', minWidth: 120, hide:true},
                {field: 'typeName', title: '类型名称', minWidth: 120},
                {field: 'majorAccidentFlg', title: '事故类型', width: 120,align:'center',templet:function (d) {
                        if(d.majorAccidentFlg == '0'){
                            return '<span class="layui-badge febs-tag-orange">非重大事故</span>'
                        }else{
                            return '<span class="layui-badge febs-tag-red">重大事故</span>'
                        }
                    }},
                {field: 'sort', title: '排序',align:'center', width: 100},
                {field: 'remark', title: '备注', minWidth: 120},
                {field: 'status', title: '状态', toolbar: '#statusOption' ,align:'center', width: 95},
                {title: '操作', toolbar: '#hazardTypeOperation-tool',align:'center', width: 150,fixed:'right'}
            ]],
            toolbar: '#hazardTypeOperation-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(hazardTypeOperationTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(hazardTypeOperationTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增隐患类型', 'hazardTypeOperation/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '95%':['600px','400px'],
                yes: function (index, layero) {
                    $('#hazardTypeOperation-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });
        }
    });

    table.on('tool(hazardTypeOperationTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.typeOperationId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改隐患类型', 'hazardTypeOperation/update/' + data.typeOperationId, {
                area: $(window).width() <= 750 ? '95%':['600px','400px'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#hazardTypeOperation-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }
    });

    form.on('switch(statusOption)', function (obj) {
        //奖品状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其id
        var id = table.cache['hazardTypeOperationTable'][index].typeOperationId;
        febs.modal.confirm('更改状态', '确认更隐患类型状态', function () {
            updateStatus(id, status);
        }, function () {
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });

    function updateStatus(typeOperationId,status) {
        let model = {
            typeOperationId: typeOperationId,
            status: status
        }
        febs.postArray(ctx + 'hazardTypeOperation/updateStatus', model, function () {
            febs.alert.success('状态更新成功');
            $query.trigger('click');
        })
    }

    // $delete.on('click', function () {
    //     var checkStatus = table.checkStatus('hazardTypeOperationTable');
    //     if (!checkStatus.data.length) {
    //         febs.alert.warn('没有选择要删除的数据！');
    //     } else {
    //         febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
    //             var ids = [];
    //             layui.each(checkStatus.data, function (key, item) {
    //                 ids.push(item.typeOperationId);
    //             });
    //             deleteIds(ids.join(','));
    //         });
    //     }
    // });

    function getQueryParams() {
        return {
            typeName: $searchForm.find('input[name="typeName"]').val()?.trim(),
            status: $searchForm.find('select[name="status"]').find('option:selected').val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'hazardTypeOperation/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});